package com.lcf.system.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.baomidou.mybatisplus.extension.toolkit.Db;
import com.lcf.framework.utils.UserUtils;
import com.lcf.framework.vo.PageVO;
import com.lcf.system.dao.GoodsDao;
import com.lcf.system.dto.GoodsDTO;
import com.lcf.system.entity.Goods;
import com.lcf.system.query.GoodsPageQuery;
import com.lcf.system.service.GoodsService;
import com.lcf.system.vo.GoodsVO;
import org.springframework.stereotype.Service;

/**
 * 商品(Goods)表服务实现类
 *
 * @author lcf
 * @since 2025-05-02 11:55:00
 */
@Service("goodsService")
public class GoodsServiceImpl extends ServiceImpl<GoodsDao, Goods> implements GoodsService {

    @Override
    public boolean addGoods(GoodsDTO dto) {
        Goods goods = BeanUtil.copyProperties(dto, Goods.class);
        goods.setUserId(UserUtils.getUserId());

        return goods.insert();
    }

    @Override
    public boolean deleteGoods(long id) {
        return removeById(id);
    }

    @Override
    public boolean updateGoods(long id, GoodsDTO dto) {
        Goods goods = BeanUtil.copyProperties(dto, Goods.class);
        goods.setId(id);
        return goods.updateById();
    }

    @Override
    public GoodsVO getGoods(long id) {
        return BeanUtil.copyProperties(getById(id), GoodsVO.class);
    }

    @Override
    public PageVO<GoodsVO> getGoodsList(GoodsPageQuery query) {
        Page<Goods> page = Db.lambdaQuery(Goods.class)
                .eq(query.getCategoryId() != null, Goods::getCategoryId, query.getCategoryId())
                .eq(query.getStoreId() != null, Goods::getStoreId, query.getStoreId())
                .page(query.toPage());
        return new PageVO<>(page, GoodsVO.class);
    }
}

